import type { RouteObjects } from '../routers/interface';

/**
 * @description 递归查询对应的路由
 * @param path 当前访问地址
 * @param routes 路由列表
 * @returns
 */
export const searchRoute = (
  path: string,
  routes: Array<RouteObjects> = [],
): RouteObjects => {
  let result: RouteObjects = {};
  for (const item of routes) {
    if (item.path === path) return item;
    if (item.children) {
      const res = searchRoute(path, item.children);
      if (Object.keys(res).length) result = res;
    }
  }
  return result;
};

/**
 * 获取需要展开的菜单
 * @param path 当前访问地址
 * @returns array
 */
export const getOpenKeys = (path: string) => {
  let newStr: string = "";
  const newArr: Array<any> = [];
  const arr = path.split("/").map((i) => "/" + i);
  for (let i = 1; i < arr.length - 1; i++) {
    newStr += arr[i];
    newArr.push(newStr);
  }
  return newArr;
};

/**
 * 使用递归处理路由菜单，生成一维数组，做菜单权限判断
 * @param routerList 所有菜单列表
 * @param newArr 菜单的一维数组
 * @returns array
 */
export const handleRouter = (
  routerList: Menu.MenuOptions[],
  newArr: string[] = [],
) => {
  routerList.forEach((item: Menu.MenuOptions) => {
    if (typeof item === "object" && item.path) {
      newArr.push(item.path);
    }
    if ( item.children && item.children.length) {
      handleRouter(item.children, newArr);
    }
  });
  return newArr;
};

/**
 * 双重递归 找出所有 面包屑 生成对象存到 redux 中，就不用每次都去递归查找了
 * @param menuList 当前菜单列表
 * @returns object
 */
export const findAllBreadcrumb = (
  menuList: Menu.MenuOptions[],
): { [key: string]: any } => {
  const handleBreadcrumbList: any = {};
  const loop = (menuItem: Menu.MenuOptions) => {
    if (menuItem?.children?.length)
      menuItem.children.forEach((item) => loop(item));
    else
      handleBreadcrumbList[menuItem.path] = getBreadcrumbList(
        menuItem.path,
        menuList,
      );
  };
  menuList.forEach((item) => loop(item));
  return handleBreadcrumbList;
};

/**
 * 递归当前路由的 所有 关联的路由，生成面包屑导航栏
 * @param path
 * @param menuList
 */
export const getBreadcrumbList = (
  path: string,
  menuList: Menu.MenuOptions[],
) => {
  const tempPath: any[] = [];
  try {
    const getNodePath = (node: Menu.MenuOptions) => {
      tempPath.push(node);
      if (node.path === path) throw new Error("End");
      if (node.children && node.children.length) {
        for (let i = 0; i < node.children.length; i++) {
          getNodePath(node.children[i]);
        }
        // 当前节点的子节点遍历完依旧没找到，则删除路径中的该节点
        tempPath.pop();
      } else {
        // 找到叶子节点时，删除路径当中的该叶子节点
        tempPath.pop();
      }
    };
    for (let i = 0; i < menuList.length; i++) {
      getNodePath(menuList[i]);
    }
  } catch (e) {
    console.log(e);
    return tempPath.map((item) => item.title);
  }
};

/**
 * 获取浏览器默认语言
 * @returns string
 */
export const getBrowserLang = () => {
  const browserLang = navigator.language
    ? navigator.language
    : (navigator as any).browserLanguage;
  let defaultBrowserLang = "";
  if (
    browserLang.toLowerCase() === "ch" ||
    browserLang.toLowerCase() === "zh" ||
    browserLang.toLowerCase() === "zh-cn"
  ) {
    defaultBrowserLang = "zh";
  } else {
    defaultBrowserLang = "en";
  }
  return defaultBrowserLang;
};
